package com.yehui.algorithm.sword;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by XuChunH on 2016/9/16.
 */
public class MoreThanHalfNum {
    public int moreThanHalfNum(int [] array) {
        if(array == null || array.length == 0){
            return 0;
        }

        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < array.length; i++) {
            Integer num = hashMap.get(array[i]);
            if(num != null){
                hashMap.put(array[i], num + 1);
            }else {
                hashMap.put(array[i], 1);
            }
        }
        int bound = array.length / 2;
        for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()){
            if(entry.getValue() > bound){
                return entry.getKey();
            }
        }
        return 0;
    }
}
